package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * 福州市电视台 解析类
 * 
 * @author Chase
 * 
 */
public class FzntvVideoAnalyse implements AnalyseNews {

	

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = { "http://www.fzntv.cn/index.php.*option=com_content&id=[0-9].*",
				"http://www.fzntv.cn/index.php.*id=[0-9].*&option=com_content"};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
			System.err.println("不符合规则");
		}

		String html = urlMeta.getHtml();

		/*
		 * 定义新闻实体需要的字段
		 */
		String title = null;
		String dateStr = null;
		String desc = null;
		Long dateLong = null;
		String commentNum = null;
		String clickNum = null;

		Document doc = Jsoup.parse(html);

		dateStr = doc.select("div.info_l").text();
		if (StringUtils.isNullOrEmpty(dateStr)) {
			dateStr = doc.select("table").text();
		}
		dateLong = DateUtils.matchDate(dateStr);// 时间

		title = doc.select("td.info_l_title").text();// 标题

		desc = doc.select("div.info_l").text(); // 描述

		if (StringUtils.isNotEmptyAll(desc)) {
			if (desc.indexOf("标签: ") != -1) {
				int indexOf = desc.indexOf("标签: ");
				int beginIndex = indexOf + "标签: ".length();
				desc = desc.substring(beginIndex);
			}
		}

		if (!StringUtils.isNullOrEmpty(desc)) {
			desc = desc.length() > 1000 ? desc.substring(0, 1000) : desc;// 小于1000字符的描述
		}

		NewsMeta meta = new NewsMeta();
		meta.setTitle(title);
		meta.setContent(desc);
		meta.setUrl(url);
		meta.setType(2);
		
		Elements tdElements = doc.select("div.info_l>table>tbody>tr>td");
		if (tdElements.size() > 8) {
			clickNum = tdElements.get(4).text();
			commentNum = tdElements.get(8).text();

			meta.setCommentNum(NumberUtils.parseInt(commentNum));
			meta.setClickNum(NumberUtils.parseInt(clickNum));
		}
		
		meta.setDate(dateLong);

		/**
		 * 解析用于更新的地址
		 */
		String updateUrl = url;
		meta.setUpdateUrl(updateUrl);

		return meta;
	}
	
	
	public NewsMeta Update(NewsMeta meta) {
		if (meta != null) {
			String updateUrl = meta.getUpdateUrl();
			if (updateUrl != null) {
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if (responseToURL != null) {
					String commentNumstr = null;
					String clickNumString = null;

					String html = responseToURL.getHtml();
					Document doc = Jsoup.parse(html);
					Elements tdElements = doc.select("div.info_l>table>tbody>tr>td");
					if (tdElements.size() > 8) {
						clickNumString = tdElements.get(4).text();
						commentNumstr = tdElements.get(8).text();
						if(!StringUtils.isNullOrEmpty(clickNumString) && !StringUtils.isNullOrEmpty(commentNumstr)) {
							try {
								meta.setCommentNum(NumberUtils.parseInt(commentNumstr));
								meta.setClickNum(NumberUtils.parseInt(clickNumString));
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					return meta;
				}
			}
		}
		return null;
	}

	
}
